%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%% Explanation of what the file does
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% 
% This function solves for hat wages and labors (and also prices hat, new 
% trade share levels and new revenue levels) of the one period temporary 
% equilibrium in the counterfactual RUV model using an extended Alvarez and 
% Lucas type tatonnement algorithm that applies in the case of DNWR. 
%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%% Inputs needed
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% 
% 1. Previous shares times baseline current shares (PastSha), size I*I*S
% 2. Hat trade costs (TAUmatrix), size I*I*S
% 3. Hat technologies (TECmatrix), size I*S
% 4. Labor shares (labshares), size I*S
% 5. IO matrix (inoutmat), size I*S*S
% 6. Trade elasticities (Smat), size 1*S
% 7. Final good consumption shares (Amat), size I*S
% 8. Previous income by sector (PastYnom), size I*S
% 9. Limit to labor hats (Llim), size I*S
% 10. Limit to wage dot (Wlim), size I*S
% 11. Deficit levels (Deficits), size I
% 12. Max number of iterations (maxiter), scalar
% 13. Tolerance for stopping (tolerance), scalar
% 14. Lambda for tatonnement algorithm (lambda), scalar
% 15. Current period W dot in baseline economy (WD), size I*S
% 16. Current period L dot in baseline economy (LD), size I*S
% 
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%% Outputs produced
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% 
% 1. Wages hat (EqWages), size I*S
% 2. Labor hat (EqLabor), size I*S
% 3. Prices hat (EqPrices), size I*S
% 4. New trade share level (EqShares), size I*I*S
% 5. New revenue levels (EqRev), size I*S
% 6. Number of iterations used (iterations), scalar
%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%% Matlab functions invoked
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% 
% 1. NEBA_PriceSolver
% 2. NEBA_RevenueSolver
% 3. NEBA_TatoNewGuess

function [EqWages,EqLabor,EqPrices,EqShares,EqRev,iterations] = ...
NECO_PeriodTatoSolver(PastSha,TAUmatrix,TECmatrix,labshares,inoutmat,...
Smat,Amat,PastYnom,Llim,Wlim,Deficits,maxiter,tolerance,lambda,WD,LD)

% Define some important variables

I              = size(Amat,1);
S              = size(Amat,2);
Lng            = ones(I,S)./LD;
Wng            = ones(I,S)./WD;
iterations     = 0;
distancebg     = 10;

% Run the contraction mapping loop part of the tatonnement algorithm 
% described in the paper

while distancebg>tolerance && iterations<maxiter
    Wg         = Wng;
    Lg         = Lng;
    [Wng,Lng]  = NEBA_TatoNewGuess(PastSha,TAUmatrix,TECmatrix,...
                 labshares,inoutmat,Smat,Amat,PastYnom.*WD.*LD,Llim,...
                 Wlim,Deficits,Wg,Lg,lambda);          
    iterations = iterations+1;
    distancebg = max(max(abs(Wng(:)-Wg(:))),max(abs(Lng(:)-Lg(:))));
end

% Assign final values to wage and labor hats and retrieve final prices
% trade shares and revenues

EqWages        = Wng;
EqLabor        = Lng;
[EqPrices,EqShares] = NEBA_PriceSolver(ones(I,S),PastSha,TAUmatrix,...
TECmatrix,EqWages,labshares,inoutmat,Smat,800,1e-8);
Vmat           = sum(EqWages.*EqLabor.*PastYnom.*WD.*LD,2) + Deficits;
EqRev          = NEBA_RevenueSolver(EqShares,Vmat,Amat,inoutmat);

% Print a warning if iterations are close or at limit

if iterations > maxiter-3
    disp("Maximum number of iterations in tatonnement reached")
end

end
